package fr.umlv.ig.extraWindows;


import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

import fr.umlv.ig.metroFactory.FileFactory;


/**
 * Represents the help window. You can find her some information of the procject.
 * 
 * @author jenseric
 * @author william
 *
 */
@SuppressWarnings("serial")
public class HelpWindow extends ExtraWindow {

	/**
	 * Initializes the windows
	 * @param owner the frame which launch the application.
	 */
	public HelpWindow(JFrame owner) {
		super(owner,"Help", true,500,500);
		init();
		dispose();
	}

	/**
	 * Initializes the components of the frame.
	 */
	private void init() {
		final JEditorPane editorPane = getEditorPane();
		JScrollPane editorScrollPane = new JScrollPane(editorPane);

		add(editorScrollPane);

		editorPane.addMouseListener(new MouseAdapter() {

			@Override
			public void mousePressed(MouseEvent e) {
				if(editorPane.getCursor().getType() == Cursor.HAND_CURSOR) {
					try {
						Desktop.getDesktop().mail(new URI("mailto:" +
								"jenseric.catbagan@gmail.com?" +
						"subject=Jacques%20Beauheur&body=Question%20for%20Jacques%20Beauheur."));
					} catch (IOException e1) {
						e1.printStackTrace();
					} catch (URISyntaxException e1) {
						e1.printStackTrace();
					}
				}
			}
		});

	}
	
	/**
	 * Returns the {@link JEditorPane} with a html content.
	 * @return the {@link JEditorPane} with a html content.
	 */
	private JEditorPane getEditorPane() {
		JEditorPane editorPane = new JEditorPane();
		editorPane.setEditable(false);
		URL helpURL = FileFactory.HELP;
		if (helpURL != null) {
			try {
				editorPane.setPage(helpURL);
			} catch (IOException e) {
				System.err.println("Attempted to read a bad URL: " + helpURL);
			}
		} else {
			System.err.println("Couldn't find file: help.html");
		}
		editorPane.setEditable(false);
		return editorPane;
	}	


	@Override
	public void start() {
		setVisible(true);
	}
}
